<?php


namespace app\service;

//上传文件 服务
use think\response\Json;
use \think\facade\Filesystem;

class uploadFileService
{
    public static function uploadImg($fileName): Json
    {
        // 获取表单上传文件
        $fileImgs = request()->file($fileName);

        //存储本地服务器
        try {
            validate(['image' => 'filesize:10240|fileExt:jpg|image:200,200,jpg'])->check($fileImgs);
            $saveName = [];
            foreach ($fileImgs as $file) {
                $filePath = Filesystem::disk('public')->putFile('image', $file);

                $saveName[] = str_replace('\\', '/', '/static/' . $filePath);
            }

            //返回数据
            return \json(['code' => 200, 'imgs' => $saveName, 'msg' => '上传成功']);
        } catch (\think\exception\ValidateException $e) {
            echo \json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }
}